package org.ThreadDemo.sync;

public class ThreadHomework2 {
    static class Counter1{
        int count;
        public void add(){
            count++;
        }
    }



    public static void main(String[] args) {
        System.out.println("....................................."+func());
    }

    public static int func(){
        Counter1 counter1= new Counter1();
        long start=System.currentTimeMillis();
        Thread thread1=new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<500000;i++){
                    System.out.println("thread1:"+counter1.count);
                    counter1.add();
                }
            }
        });
        thread1.start();




        Thread thread2=new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<500000;i++){
                    System.out.println("thread2::::::::"+counter1.count);
                    counter1.add();
                }
            }
        });
        thread2.start();


        try {
            //此处的join方法是,方便thread1,thread2
            //一起调用结束之后，在main线程当中统计时间
            thread1.join();
            thread2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        long end=System.currentTimeMillis();
        System.out.println(end-start);
        return counter1.count;
    }
}

